﻿using System;
using System.Architecture;
using System.Collections;
using System.Collections.Data;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Xml.Linq;
using HDK.API.Syndication;
using HDK.UI.Controls;
using Microsoft.Practices.Prism.Regions;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace HDK.Demo.Pages.Views
{
    [Shared]
    [ExportView("Pages/GIT", "GIT", new string[] { "MainRegion" })]
    public sealed partial class GroupedIncrementalTest : UserControl, IPageView
    {
        public GroupedIncrementalTest()
        {
            this.InitializeComponent();
        }

        public string Title
        {
            get { return "GIT"; }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            sz.ToggleActiveView();
        }

        #region SemanticZoom

        private object semanticZoomOutItemSource;

        private void SemanticZoomViewChangeStarted(object sender, SemanticZoomViewChangedEventArgs e)
        {
            var zoom = (SemanticZoom)sender;
            var zoomIn = (GridView)zoom.ZoomedInView;
            var zoomOut = (GridView)zoom.ZoomedOutView;

            zoomOut.ItemsSource = groups.View.CollectionGroups;
            if (e.IsSourceZoomedInView)
            {
                ScrollFromSourceToDestination(e, zoomIn, zoomOut);
                zoomOut.Visibility = Visibility.Visible;
            }
            else
            {
                if (semanticZoomOutItemSource != null)
                {
                    e.SourceItem.Item = semanticZoomOutItemSource;
                    semanticZoomOutItemSource = null;
                }
                zoomOut.Visibility = Visibility.Collapsed;
                ScrollFromSourceToDestination(e, zoomOut, zoomIn);
            }
        }

        private void OnZoomOutItemClick(object sender, ItemClickEventArgs e)
        {
            semanticZoomOutItemSource = sender;
            sz.ToggleActiveView();
        }


        private static void ScrollFromSourceToDestination(SemanticZoomViewChangedEventArgs e, GridView source, GridView destination)
        {
            int index = source.Items.IndexOf(e.SourceItem.Item);
            if (index < 0 || destination.Items.Count < index || destination.Items.Count == 0)
                return;
            if (destination.Items[index] == null)
                return;
            destination.ScrollIntoView(destination.Items[index], ScrollIntoViewAlignment.Default);

        }

        #endregion
    }


    public class SampleDataSource : SimpleDataListSource<string>
    {
        protected override async Task<IList<string>> FetchItemsAsync()
        {
            List<string> data = new List<string>();
            int count = 100;
            for (int i = 0; i < count; i++)
            {
                data.Add(Convert.ToString(i));
            }
            return data;
        }
    }

    [Shared]
    [ExportViewModel("GIT")]
    public class SyndicationRepositoryViewModel : NavigationAwareViewModel
    {
        ObservableCollection<string> m_Data;
        public ObservableCollection<string> Data { get { return m_Data; } set { SetProperty(ref m_Data, value); } }

        public GrouppedObservableCollection<char, string> m_Groups;
        public GrouppedObservableCollection<char, string> Groups { get { return m_Groups; } set { SetProperty(ref m_Groups, value); } }

        public ICommand AddNewItemCommand { get; set; }

        public SyndicationRepositoryViewModel()
        {
            AddNewItemCommand = new DelegateCommand(() => Data.Add((Data.Count + 1).ToString()));

            Data = new ObservableCollection<string>();

            Groups = new GrouppedObservableCollection<char, string>(Data as INotifyCollectionChanged, (s) => s[0]);

            //for (int i = 0; i < 3; i++)
            //    Data.Add(i.ToString());
        }
    }
}
